import styles from './index.module.scss';
import classNames from 'classnames';
import { useEffect, useRef } from 'react';

interface TextAreaProps
  extends React.DetailedHTMLProps<
    React.TextareaHTMLAttributes<HTMLTextAreaElement>,
    HTMLTextAreaElement
  > {
  value: string;
}

export default function Textarea(props: TextAreaProps) {
  const iptRef = useRef<HTMLTextAreaElement>(null);
  const { maxLength = 100, className, value, autoFocus = true, ...restProps } = props;

  useEffect(() => {
    iptRef.current?.setSelectionRange(-1, -1);
  }, []);

  return (
    <div className={styles.root}>
      <textarea
        ref={iptRef}
        {...restProps}
        value={value}
        className={classNames('textarea', className)}
        maxLength={maxLength}
        autoFocus={autoFocus}
      />
      <div className="count">
        {value.length}/{maxLength}
      </div>
    </div>
  );
}
